package com.dragon.blelibrary.manager;



import android.text.TextUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;

/**
 * 存放需要使能的通知
 */
 class GattServiceCharacteristic {
    /**保存UUID与name的关系 <uuid,name>*/
    private static HashMap<String, String> uuidToName = new HashMap<>();
    /**不能使能的通知*/
    private static List<String> unNotificationList = new ArrayList<String>();
    static{
        unNotificationList.add("00002a22-0000-1000-8000-00805f9b34fb");
        unNotificationList.add("00002a37-0000-1000-8000-00805f9b34fb");
        unNotificationList.add("00002a4d-0000-1000-8000-00805f9b34fb");
        unNotificationList.add("00033333-0000-2000-8000-00805f9b0221");
    }
    /**手动动使能*/
    private static List<UUID[]> characteristicMap = new ArrayList<>();
    /**自动使能的集合*/
    private static List<UUID[]> autoCharacteristicMap = new ArrayList<>();

    /**
     * <UUID,UUID> <Service,Characteristic>
     * @return
     */
    public static List<UUID[]> getNotificationMap(){
        return characteristicMap;
    }

    /**
     * 获取需要自动使能的通知列表
     * @return
     */
    public static List<UUID[]> getAutoNotificationMap(){
        return autoCharacteristicMap;
    }
    /**
     * 手动添加单条通知特性
     * @param serviceNeme 给service 的命名
     * @param serviceUUid service UUID
     * @param notifyName 给notify 的命名
     * @param notifyUUid notify UUID
     */
    public static void putNotificationMap(String serviceNeme, UUID serviceUUid, String notifyName, UUID notifyUUid){
        HashMap<UUID, UUID> tmpMap = new HashMap<>();
        tmpMap.put(serviceUUid,notifyUUid);
        uuidToName.put(serviceUUid.toString(),serviceNeme);
        uuidToName.put(notifyUUid.toString(),notifyName);

        UUID[] arrUUID = new UUID[2];
        arrUUID[0] = serviceUUid;
        arrUUID[1] = notifyUUid;
        characteristicMap.add(arrUUID);
    }

    /**
     * 自动扫描得到的所有的通知的，并保存下来
     * @param serviceUUid
     * @param notifyUUid
     */
    public static void putAutoNotificationMap(UUID serviceUUid, UUID notifyUUid){


        if(!uuidToName.containsKey(serviceUUid.toString())){
            uuidToName.put(serviceUUid.toString(),serviceUUid.toString());
        }
        if(!uuidToName.containsKey(notifyUUid.toString())){
            uuidToName.put(notifyUUid.toString(),notifyUUid.toString());
        }
        if(unNotificationList.contains(notifyUUid.toString())){//去掉一些不能使能的通知
            return;
        }
        UUID[] arrUUID = new UUID[2];
        arrUUID[0] = serviceUUid;
        arrUUID[1] = notifyUUid;

        autoCharacteristicMap.add(arrUUID);

    }

    /**
     * 添加一些不能使能的通知
     * @param notifyUUID
     */
    public static void addUnNotification(UUID notifyUUID){
        unNotificationList.add(notifyUUID.toString());
    }

    /**
     * 通过UUID找到UUID的名称，用于打印log
     * @param uuid
     * @return
     */
    public static String findNameByUUid(String uuid){
        String name = uuidToName.get(uuid);
        if(TextUtils.isEmpty(name)){
            name = "UNKNOWN_UUid = " + uuid;
        }
        return name;
    }

    /**
     * 给UUID名个名称方便打log
     * @param uuid
     * @param name
     */
    public static void putUuidToName(UUID uuid, String name){
        uuidToName.put(uuid.toString(),name);
    }

    /**
     * 清除
     */
    public static void clearNotify(){
        if(unNotificationList != null)
            unNotificationList.clear();
//        if(uuidToName != null)
//            uuidToName.clear();
    }
}
